package morn.core.components
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Matrix;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	/**
	 * 位图文本，用于显示像素文字
	 * @author WLDragon 2014-02-27
	 */
	public class BitmapText extends Component
	{
		
		public function BitmapText()
		{
		
		}
		
		override protected function createChildren():void
		{
		
		}
		
		override protected function initialize():void
		{
			_contentWidth = 360;
			_contentHeight = 24;
		}
		
		/**
		 * 获取像素文字(仅限于一行)
		 * 返回的图片文字高度为24像素，只有这个尺寸的
		 * @param	content  内容文字
		 * @param	color 颜色
		 * @return
		 */
		public function getPixelWord(content:String):BitmapData
		{
			var tf:TextFormat = new TextFormat("宋体", 12, _color,_bold,_italic);
			var txt:TextField = new TextField;
			txt.autoSize = TextFieldAutoSize.LEFT;
			txt.defaultTextFormat = tf;
			txt.text = content;
			
			var w:int = 24 * txt.length;
			var b:BitmapData = new BitmapData(w, 24, true, 0x000000);
			b.draw(txt, new Matrix(1, 0, 0, 1, -2, -2));
			
			//因为draw的源是BitmapData,缩放才会产生锯齿,所以用bb再draw一次
			var bb:BitmapData = new BitmapData(b.width, b.height, true, 0x000000);
			bb.draw(b, new Matrix(2, 0, 0, 2));
			
			b.dispose();
			b = null;
			
			return bb;
		}
		
		private function changeText():void
		{
			while (numChildren > 0)
			{
				removeChildAt(0);
			}
			
			if (_text == null || _text == "")
				return;
			
			//如果有\n则以\n分行,否则按宽度自适应
			if (_text.indexOf('\n') != -1)
			{
				var arr:Array = _text.split('\n');
				for (var j:int = 0; j < arr.length; j++) 
				{
					addBmp(arr[j],j);
				}
			}
			else
			{
				//每行的字数
				var cn:int = width / 24 >> 0;
				cn <= 0 ? 1 : cn;
				var p:int = 0;
				var i:int = 0;
				while (p < _text.length)
				{
					var s:String = _text.substr(p, cn);
					addBmp(s,i);
					p += cn;
					i++;
				}
			}
			
			//以后添加数组记录bmp，用于逐字显示
		}
		
		private function addBmp(content:String,index:int):void
		{
			var bmp:Bitmap = new Bitmap(getPixelWord(content));
			bmp.y = (_leading + 24) * index;
			addChild(bmp);
		}
		
		public function get text():String
		{
			return _text;
		}
		
		public function set text(value:String):void
		{
			_text = value;
			callLater(changeText);
		}
		
		public function get realSize():Number
		{
			return _realSize;
		}
		
		public function set realSize(value:Number):void
		{
			//使用缩放实现
			scale = value / 12;
			_realSize = value;
			callLater(changeText);
		}
		
		public function get color():uint
		{
			return _color;
		}
		
		public function set color(value:uint):void
		{
			_color = value;
			callLater(changeText);
		}
		
		public function get leading():int
		{
			return _leading;
		}
		
		public function set leading(value:int):void
		{
			_leading = value;
			callLater(changeText);
		}
		
		override public function set width(value:Number):void
		{
			super.width = value;
			callLater(changeText);
		}
		
		override public function set height(value:Number):void
		{
			super.height = value;
			callLater(changeText);
		}
		
		public function get bold():Boolean 
		{
			return _bold;
		}
		
		public function set bold(value:Boolean):void 
		{
			_bold = value;
			callLater(changeText);
		}
		
		public function get italic():Boolean 
		{
			return _italic;
		}
		
		public function set italic(value:Boolean):void 
		{
			_italic = value;
			callLater(changeText);
		}
		
		/**内容*/
		protected var _text:String;
		/**文字颜色*/
		protected var _color:uint;
		/**实际文字的大小,像素文字是实际文字的2倍*/
		protected var _realSize:Number;
		/**行距*/
		protected var _leading:int;
		/**粗体*/
		protected var _bold:Boolean;
		/**斜体*/
		protected var _italic:Boolean;
	}

}